import { createRouter, createWebHistory } from 'vue-router';
import Home from '@/views/Home.vue';
import StudentManagement from '@/views/teacher/StudentManagement.vue';
import TeachingContent from '@/views/teacher/TeachingContent.vue';
import LearningReport from '@/views/teacher/LearningReport.vue';
import FilePreview from '@/views/FilePreview.vue';
// 家长功能页面
import ChildProgress from '@/views/parent/ChildProgress.vue';
import StudyPlan from '@/views/parent/StudyPlan.vue';
import ContactTeacher from '@/views/parent/ContactTeacher.vue';
// 儿童功能页面
import ListeningLearning from '@/views/child/ListeningLearning.vue';
import ReadingLearning from '@/views/child/ReadingLearning.vue';
import GameLearning from '@/views/child/GameLearning.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  // 教师功能路由
  {
    path: '/teacher/students',
    name: 'StudentManagement',
    component: StudentManagement,
    meta: { requiresAuth: true, role: 'teacher' }
  },
  {
    path: '/teacher/content',
    name: 'TeachingContent',
    component: TeachingContent,
    meta: { requiresAuth: true, role: 'teacher' }
  },
  {
    path: '/teacher/reports',
    name: 'LearningReport',
    component: LearningReport,
    meta: { requiresAuth: true, role: 'teacher' }
  },
  {
    path: '/file/preview/:fileName',
    name: 'FilePreview',
    component: FilePreview
  },
  // 家长功能路由
  {
    path: '/parent/progress',
    name: 'ChildProgress',
    component: ChildProgress,
    meta: { requiresAuth: true, role: 'parent' }
  },
  {
    path: '/parent/plan',
    name: 'StudyPlan',
    component: StudyPlan,
    meta: { requiresAuth: true, role: 'parent' }
  },
  {
    path: '/parent/contact',
    name: 'ContactTeacher',
    component: ContactTeacher,
    meta: { requiresAuth: true, role: 'parent' }
  },
  // 儿童功能路由
  {
    path: '/child/listening',
    name: 'ListeningLearning',
    component: ListeningLearning,
    meta: { requiresAuth: true, role: 'child' }
  },
  {
    path: '/child/reading',
    name: 'ReadingLearning',
    component: ReadingLearning,
    meta: { requiresAuth: true, role: 'child' }
  },
  {
    path: '/child/game',
    name: 'GameLearning',
    component: GameLearning,
    meta: { requiresAuth: true, role: 'child' }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    // 只从sessionStorage读取，确保每个标签页独立会话
    const userData = sessionStorage.getItem('user');
    
    if (!userData) {
      alert('请先登录');
      next('/');
      return;
    }
    
    try {
      const user = JSON.parse(userData);
      if (!user || !user.role) {
        alert('用户信息不完整，请重新登录');
        // 清除无效的用户数据
        sessionStorage.removeItem('user');
        sessionStorage.removeItem('token');
        next('/');
        return;
      }
      
      // 检查角色权限
      if (to.meta.role && user.role !== to.meta.role) {
        console.warn(`权限检查失败: 用户角色=${user.role}, 需要角色=${to.meta.role}`);
        alert('您没有权限访问此页面');
        next('/');
        return;
      }
    } catch (error) {
      console.error('解析用户数据失败:', error);
      alert('用户信息格式错误，请重新登录');
      // 清除无效的用户数据
      sessionStorage.removeItem('user');
      sessionStorage.removeItem('token');
      next('/');
      return;
    }
  }
  next();
});

export default router;
